package com.bcu.calculus.service.serviceImpl;

import com.bcu.calculus.entity.Act;
import com.bcu.calculus.entity.ActNumber;
import com.bcu.calculus.entity.Message;
import com.bcu.calculus.mapper.ActMapper;
import com.bcu.calculus.mapper.UnpassMapper;
import com.bcu.calculus.service.ActService;
import com.bcu.calculus.utils.ActUtil;
import com.bcu.calculus.utils.SendUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
public class ActServiceImpl implements ActService {

    @Resource
    private ActMapper actMapper;
    @Resource
    private UnpassMapper unpassMapper;


    @Override
    public Message<?> insert(ActNumber actNumber) throws ParseException {
        Act act = ActUtil.change(actNumber);
        Integer actId = ActUtil.genActId();
        act.setActId(actId);
        HashMap map = new HashMap<String,Object>();
        map.put("actId",actId);
        //第一次添加更新时间是添加时的时间
        act.setActUpdateTime(new Date());
        return actMapper.insert(act) > 0 ? Message.success(null).add(map) : Message.fail(null).add(map);
    }

    @Override
    public Message<?> deleteByPrimaryKey(Integer actId) {
        return actMapper.deleteByPrimaryKey(actId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> updateByPrimaryKey(ActNumber actNumber) throws ParseException {
        Act act = ActUtil.change(actNumber);
        act.setActUpdateTime(new Date());
        return actMapper.updateByPrimaryKeySelective(act) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<Act> selectByPrimaryKey(Integer actId) {
        Act act = actMapper.selectByPrimaryKey(actId);
        act.setUnpass(unpassMapper.selectOneByActId(act.getActId()));
        return Message.success(null).add(act);
    }

    @Override
    public Message<List<Act>> selectAllAct() {
        List<Act> actList = actMapper.selectAllAct();
        for (Act act:actList) {
            act.setUnpass(unpassMapper.selectOneByActId(act.getActId()));
        }
        return Message.success(null).add(actList);
    }

    @Override
    public Message<List<Act>> selectActsBySendId(String sendId) {

        List<Act> actList = actMapper.selectActsBySendId(sendId);
        for (Act act:actList) {
            act.setUnpass(unpassMapper.selectOneByActId(act.getActId()));
        }

        return Message.success(null).add(actList);
    }
}
